package emas.client.handler;

import com.google.gwt.core.client.Scheduler;

public class UpdateHandler {

	private boolean active = false;

	private DataHandler dataHandler;

	private long updateIntervallMS = 20000;

	public UpdateHandler(DataHandler handler) {
		this.dataHandler = handler;
	}

	public long getUpdateIntervallMS() {
		return updateIntervallMS;
	}

	public void setUpdateIntervallMS(long updateIntervallMS) {
		stopUpdateHandler();
		this.updateIntervallMS = updateIntervallMS;
		startUpdateHandler();
	}

	public void stopUpdateHandler() {
		active = false;
	}
	
	public void startUpdateHandler() {
		active = true;
		startCommand();
	}

	private void startCommand() {
		
		Scheduler.RepeatingCommand command = new Scheduler.RepeatingCommand() {
			
			@Override
			public boolean execute() {
				dataHandler.getLocations();
				return active;
			}
		};
		
		Scheduler.get().scheduleFixedDelay(command, (int) updateIntervallMS);
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
}
